/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.floor;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.I18nUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.floor.EleSourceFloored;
import com.thinkgem.jeesite.modules.source.service.floor.EleSourceFlooredService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 已登记楼层资源Controller
 * @author ZC
 * @version 2017-06-19
 */
@Controller
@RequestMapping(value = "${adminPath}/source/floor/eleSourceFloored")
public class EleSourceFlooredController extends BaseController {

	@Autowired
	private EleSourceFlooredService eleSourceFlooredService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public EleSourceFloored get(@RequestParam(required=false) String id) {
		EleSourceFloored entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceFlooredService.get(id);
		}
		if (entity == null){
			entity = new EleSourceFloored();
		}
		return entity;
	}
	
	@RequiresPermissions("source:floor:eleSourceFloored:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceFloored eleSourceFloored, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());		
		}		
		eleSourceFloored.setUsers(list);
		Page<EleSourceFloored> page = eleSourceFlooredService.findPage(new Page<EleSourceFloored>(request, response), eleSourceFloored); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		return "modules/source/floor/eleSourceFlooredList";
	}

	@RequiresPermissions("source:floor:eleSourceFloored:view")
	@RequestMapping(value = "form")
	public String form(EleSourceFloored eleSourceFloored, Model model,HttpServletRequest request) {
		model.addAttribute("eleSourceFloored", eleSourceFloored);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		model.addAttribute("language", locale.getLanguage());
		if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
			model.addAttribute("langCode", locale.getLanguage()+"-cn");
		}else{
			model.addAttribute("langCode", locale.getLanguage());
		}
		return "modules/source/floor/eleSourceFlooredForm";
	}

	@RequiresPermissions("source:floor:eleSourceFloored:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceFloored eleSourceFloored, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, eleSourceFloored)){
			return form(eleSourceFloored, model,request);
		}
		eleSourceFlooredService.save(eleSourceFloored);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存已登记楼层成功";
		if(Global.US.equals(locale)){
			msg = "floored saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/floor/eleSourceFloored/?repage";
	}
	
	@RequiresPermissions("source:floor:eleSourceFloored:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceFloored eleSourceFloored, RedirectAttributes redirectAttributes) {
		eleSourceFlooredService.delete(eleSourceFloored);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除已登记楼层成功";
		if(Global.US.equals(locale)){
			msg = "floored deleted";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/floor/eleSourceFloored/?repage";
	}

}